package com.ncmmall.mall.controller.business.express;

import com.ncmmall.application.business.express.ExpressApplication;
import com.ncmmall.application.securitys.logs.OperationLogApplication;
import com.feijin.commons.webs.Webs;
import com.ncmmall.domain.business.express.Express;
import com.ncmmall.domain.business.express.QExpress;
import com.ncmmall.mall.controller.securitys.shiro.Securitys;
import com.ncmmall.mall.params.PageParams;
import com.ncmmall.querychannel.QueryPage;
import com.ncmmall.querychannel.QueryPageRequest;
import com.ncmmall.querychannel.service.QueryChannelService;
import com.google.common.collect.ImmutableMap;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 物流管理
 *
 * @author likaikai
 * @date 2019-07-11 11:18
 */
@Controller
@RequestMapping("/mall/express")
@Slf4j
public class ExpressController {

    @Autowired
    private QueryChannelService queryChannelService;

    @Autowired
    private OperationLogApplication logApplication;

    @Autowired
    private ExpressApplication expressApplication;

    /**
     * 物流列表
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(Model model, PageParams pageParams, Express params) {

        QueryPageRequest pageRequest = new QueryPageRequest().setPageNo(pageParams.getPageNo()).setPageSize(pageParams.getPageSize());

        Map<String, Object> map = new HashMap<>(2);
        String sql = buildSql("select * from mall_express where 1 = 1", map, params);

        QueryPage<Express> page = queryChannelService.queryPageEntity(sql, pageRequest.setParams(map), Express.class);

        model.addAttribute("page", page);
        model.addAttribute("params", params);

        return "mall/express/list";
    }

    /**
     * 条件查询
     */
    private String buildSql(String sql, Map<String, Object> map, Express params) {

        if (StringUtils.isNotBlank(params.getName())) {
            sql += " and name like :name";
            map.put("name", "%" + params.getName() + "%");
        }

        if (params.getStatus() > 0) {
            sql += " and status = :status";
            map.put("status", params.getStatus());
        }

        return sql + " order by createTime desc";
    }

    @RequestMapping(value = "/create", method = RequestMethod.GET)
    private String create(Model model) {
        model.addAttribute("express", new Express()).addAttribute("_method", "post");
        return "mall/express/form";
    }

    /**
     * 创建物流
     */
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> create(HttpServletRequest request, Express express) {
        try {
            if (!Webs.isAjaxRequest(request)) {
                throw new IllegalArgumentException("request must be ajax");
            }

            expressApplication.create(express);

            logApplication.eventOfSystem(Securitys.currentUser(), "添加物流渠道", Securitys.getIp(request));

            return ImmutableMap.of("status", true, "message", "创建成功");

        } catch (IllegalArgumentException e) {
            return ImmutableMap.of("status", false, "message", e.getMessage());
        } catch (Exception e) {
            log.error("物流创建异常", e);
            return ImmutableMap.of("status", false, "message", "创建异常");
        }
    }

    @RequestMapping(value = "/{id}/modify", method = RequestMethod.GET)
    private String modify(Model model, @PathVariable("id") long id) {
        model.addAttribute("express", queryChannelService.findOne(QExpress.express.id.eq(id), Express.class)).addAttribute("_method", "PUT");
        return "mall/express/form";
    }

    /**
     * 编辑物流
     */
    @RequestMapping(value = "/{id}/modify", method = RequestMethod.PUT)
    @ResponseBody
    public Map<String, Object> modify(HttpServletRequest request, @RequestHeader("X-Requested-With") String requestWith, Express express) {
        try {
            if (!Webs.isAjaxRequest(requestWith)) {
                throw new IllegalArgumentException("request must be ajax");
            }
            expressApplication.modify(express);
            logApplication.eventOfSystem(Securitys.currentUser(), "编辑物流渠道", Securitys.getIp(request));
            return ImmutableMap.of("status", true, "message", "编辑成功");
        } catch (IllegalArgumentException e) {
            return ImmutableMap.of("status", false, "message", e.getMessage());
        } catch (Exception e) {
            log.error("物流编辑异常", e);
            return ImmutableMap.of("status", false, "message", "编辑异常");
        }
    }

    /**
     * 下架物流
     */
    @RequestMapping(value = "/disable", method = RequestMethod.PUT)
    @ResponseBody
    public Map<String, Object> disable(HttpServletRequest request, Express express) {
        try {
            if (!Webs.isAjaxRequest(request)) {
                throw new IllegalArgumentException("request must be ajax");
            }
            expressApplication.disable(express.getId());
            logApplication.eventOfSystem(Securitys.currentUser(), "下架物流渠道", Securitys.getIp(request));
            return ImmutableMap.of("status", true, "message", "下架成功");
        } catch (Exception e) {
            log.error("物流下架异常", e);
            return ImmutableMap.of("status", false, "message", "下架异常");

        }
    }

    /**
     * 批量下架物流
     */
    @RequestMapping(value = "/disables", method = RequestMethod.PUT)
    @ResponseBody
    public Map<String, Object> disables(HttpServletRequest request, @RequestHeader("X-Requested-With") String requestWith, String data) {
        try {
            if (!Webs.isAjaxRequest(requestWith)) {
                throw new IllegalArgumentException("request must be ajax");
            }
            expressApplication.disables(data);
            logApplication.eventOfSystem(Securitys.currentUser(), "批量下架物流渠道", Securitys.getIp(request));
            return ImmutableMap.of("status", true, "message", "下架成功");
        } catch (Exception e) {
            log.error("物流下架异常", e);
            return ImmutableMap.of("status", false, "message", "下架异常");
        }
    }


    /**
     * 上架物流
     */
    @RequestMapping(value = "/enable", method = RequestMethod.PUT)
    @ResponseBody
    public Map<String, Object> enable(HttpServletRequest request, @RequestHeader("X-Requested-With") String requestWith, Express express) {
        try {
            if (!Webs.isAjaxRequest(requestWith)) {
                throw new IllegalArgumentException("request must be ajax");
            }
            expressApplication.enabled(express.getId());
            logApplication.eventOfSystem(Securitys.currentUser(), "上架物流渠道", Securitys.getIp(request));
            return ImmutableMap.of("status", true, "message", "上架成功");
        } catch (Exception e) {
            log.error("物流上架异常", e);
            return ImmutableMap.of("status", false, "message", "上架异常");
        }
    }


    /**
     * 批量上架物流
     */
    @RequestMapping(value = "/enables", method = RequestMethod.PUT)
    @ResponseBody
    public Map<String, Object> enables(HttpServletRequest request, @RequestHeader("X-Requested-With") String requestWith, String data) {
        try {
            if (!Webs.isAjaxRequest(requestWith)) {
                throw new IllegalArgumentException("request must be ajax");
            }
            expressApplication.enables(data);
            logApplication.eventOfSystem(Securitys.currentUser(), "批量上架物流渠道", Securitys.getIp(request));
            return ImmutableMap.of("status", true, "message", "上架成功");
        } catch (Exception e) {
            log.error("物流批量上架异常", e);
            return ImmutableMap.of("status", false, "message", "上架异常");
        }
    }

    /**
     * 删除物流
     */
    @RequestMapping(value = "/remove", method = RequestMethod.DELETE)
    @ResponseBody
    public Map<String, Object> remove(HttpServletRequest request, Express express) {
        try {
            if (!Webs.isAjaxRequest(request)) {
                throw new IllegalArgumentException("request must be ajax");
            }
            expressApplication.remove(express.getId());
            logApplication.eventOfSystem(Securitys.currentUser(), "删除物流渠道", Securitys.getIp(request));
            return ImmutableMap.of("status", true, "message", "删除成功");
        } catch (Exception e) {
            log.error("物流删除异常", e);
            return ImmutableMap.of("status", false, "message", "删除异常");
        }
    }

}
